package edu.jmu.service.attendance.impl;

import java.util.Date;

import org.springframework.stereotype.Service;

import edu.jmu.bean.attendance.ManualSign;
import edu.jmu.service.attendance.ManualSignService;
import edu.jmu.bean.attendance.SignTag;
import edu.jmu.service.base.DaoSupport;

@Service
public class ManualSignServiceBean extends DaoSupport<ManualSign> implements ManualSignService{
	
	@Override
	public ManualSign onDutyTime(Date dutytime1,Date dutytime2) {
		String jpql = "select o from ManualSign o where o.signTime >= ?1 and o.signTime <= ?2";
		ManualSign manualSign = (ManualSign) em.createQuery(jpql).setParameter(1, dutytime1).setParameter(2, dutytime2).getSingleResult();
//		super.getScrollData(0, 20, jpql, new Date[]{dutytime1,dutytime2);
		return manualSign;
	}
	@Override
	public ManualSign findHistory(ManualSign user) {
		String wherejpql = "o.user=?";
		ManualSign manualSign = (ManualSign) em.createQuery(wherejpql).setParameter(1, user).getSingleResult();
		return manualSign;
	}

	@Override
	public int defferDate(Date starttime, Date endtime) {
		// TODO Auto-generated method stub
		return 0;
	}
	

	public int getSignStatus(SignTag signTag) {
		String jpql = "select count(*) from ManualSign o group by o.userId where o.signTime = ?1";
		Long count = (Long)em.createQuery(jpql).setParameter(1, signTag).getSingleResult();
		return Integer.parseInt(count.toString());
	}
	

	@Override
	public double getUserAttendance(SignTag signTag) {
		String jpql = "select count(*) from ManualSign o group by o.userId where o.signTime = ?1";
		Long count = (Long)em.createQuery(jpql).setParameter(1, signTag).getSingleResult();
		return Integer.parseInt(count.toString());
         //String wherejpql ="select o.userId,(select Count(1)/2 from ManualSign c where c.userId=o.userId and c.signTime >= To_Date('2012-05-07','yyyy-mm-dd')and c.signTime <=To_Date('2012-05-09','yyyy-mm-dd')/(select To_Date('2012-05-09','yyyy-mm-dd')-To_Date('2012-05-07','yyyy-mm-dd')from dual))";
		//String hejpql = "select(select count(*) from Manualsign  o where o.signTime>starttime and o.signTime < endTime and o.onDutyTime > 9:00)/(select count(*) form ManualSign o where o.signTime > starttime and o.onDutyTime < endtime)";

		
	}

	/*@Override
	public int getUserLeaveEarly(int userId, Date starttime, Date endtime) {
		String jpql="select count(1) from manualSign o where o.signTime < ?1 and o.signTag = 0 and o.userId = '员工ID' and workTime between startTime and endTime";
		ManualSign manualSign = (ManualSign)em.createQuery(jpql).setParameter(1, userId).setParameter(2, starttime).setParameter(2, endtime).getSingleResult();
		return 0;
	}

	@Override
	public int getUserAbsenteeism(int userId, Date starttime, Date endtime) {
		String wherejpql = "select count(*) from ManualSign o group by o.userId where o.signTime > starttime and o.signTime < endTime and o.onDutyTime is null";
		ManualSign manualSign = (ManualSign)em.createQuery(wherejpql).setParameter(1, userId).setParameter(2, starttime).setParameter(2, endtime).getSingleResult();
		return 0;
	}
	@Override
	public ManualSign findManger(Date starttime, Date endtime) {
		String jpql = "select o from UserInfo where UserId in(select UserId from ManualSign where signtime between Convert(Date,'" + starttime + "') and Convert(Date,'" + endtime + "'))";
		ManualSign manualSign = (ManualSign) em.createQuery(jpql).setParameter(1, starttime).setParameter(2, endtime).getSingleResult();
		return manualSign;
	}*/
	

	
}